# Android API访问修复脚本
# 解决Android应用无法访问API的问题

Write-Host "=== Android API访问修复脚本 ===" -ForegroundColor Green
Write-Host ""

# 检查必要文件是否存在
$androidManifest = "src-tauri\gen\android\app\src\main\AndroidManifest.xml"
$networkConfig = "src-tauri\gen\android\app\src\main\res\xml\network_security_config.xml"

if (-not (Test-Path $androidManifest)) {
    Write-Host "错误: 找不到AndroidManifest.xml文件" -ForegroundColor Red
    Write-Host "请先运行 'npm run tauri android init' 初始化Android项目" -ForegroundColor Yellow
    exit 1
}

Write-Host "1. 检查Android权限配置..." -ForegroundColor Cyan

# 读取AndroidManifest.xml
$manifestContent = Get-Content $androidManifest -Raw

# 检查必要权限
if ($manifestContent -match 'android.permission.INTERNET') {
    Write-Host "✓ INTERNET权限已配置" -ForegroundColor Green
} else {
    Write-Host "缺少INTERNET权限" -ForegroundColor Red
}

if ($manifestContent -match 'android:usesCleartextTraffic') {
    Write-Host "✓ 明文流量已允许" -ForegroundColor Green
} else {
    Write-Host "警告: 未启用明文流量" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "2. 更新网络安全配置..." -ForegroundColor Cyan

# 确保网络安全配置目录存在
$networkConfigDir = Split-Path $networkConfig -Parent
if (-not (Test-Path $networkConfigDir)) {
    New-Item -ItemType Directory -Path $networkConfigDir -Force | Out-Null
    Write-Host "创建目录: $networkConfigDir" -ForegroundColor Yellow
}

# 创建网络安全配置内容
$xmlContent = '<?xml version="1.0" encoding="utf-8"?>' + "`n"
$xmlContent += '<network-security-config>' + "`n"
$xmlContent += '    <base-config cleartextTrafficPermitted="true">' + "`n"
$xmlContent += '        <trust-anchors>' + "`n"
$xmlContent += '            <certificates src="system"/>' + "`n"
$xmlContent += '            <certificates src="user"/>' + "`n"
$xmlContent += '        </trust-anchors>' + "`n"
$xmlContent += '    </base-config>' + "`n"
$xmlContent += '    <domain-config cleartextTrafficPermitted="true">' + "`n"
$xmlContent += '        <domain includeSubdomains="true">localhost</domain>' + "`n"
$xmlContent += '        <domain includeSubdomains="true">127.0.0.1</domain>' + "`n"
$xmlContent += '        <domain includeSubdomains="true">10.0.2.2</domain>' + "`n"
$xmlContent += '        <domain includeSubdomains="true">192.168.1.1</domain>' + "`n"
$xmlContent += '        <domain includeSubdomains="true">api.jisuapi.com</domain>' + "`n"
$xmlContent += '        <domain includeSubdomains="true">jisuapi.com</domain>' + "`n"
$xmlContent += '    </domain-config>' + "`n"
$xmlContent += '</network-security-config>' + "`n"

Set-Content -Path $networkConfig -Value $xmlContent -Encoding UTF8
Write-Host "✓ 网络安全配置已更新" -ForegroundColor Green

Write-Host ""
Write-Host "3. 检查前端API配置..." -ForegroundColor Cyan

$lotteryApiFile = "src\lotteryApi.js"
if (Test-Path $lotteryApiFile) {
    $apiContent = Get-Content $lotteryApiFile -Raw
    if ($apiContent -match "isTauri") {
        Write-Host "✓ API配置已正确设置" -ForegroundColor Green
    } else {
        Write-Host "警告: API配置可能需要更新" -ForegroundColor Yellow
    }
} else {
    Write-Host "错误: 找不到lotteryApi.js文件" -ForegroundColor Red
}

Write-Host ""
Write-Host "4. 清理构建缓存..." -ForegroundColor Cyan

$androidBuildDir = "src-tauri\gen\android\app\build"
if (Test-Path $androidBuildDir) {
    Remove-Item $androidBuildDir -Recurse -Force
    Write-Host "✓ Android构建缓存已清理" -ForegroundColor Green
} else {
    Write-Host "构建缓存目录不存在" -ForegroundColor Yellow
}

Write-Host ""
Write-Host "=== 修复完成 ===" -ForegroundColor Green
Write-Host ""
Write-Host "接下来的步骤:" -ForegroundColor Cyan
Write-Host "1. 运行 'npm run build' 重新构建前端" -ForegroundColor White
Write-Host "2. 运行 '.\build_android_workaround.ps1' 重新构建Android应用" -ForegroundColor White
Write-Host "3. 安装生成的APK文件到Android设备" -ForegroundColor White
Write-Host ""